package com.logicbus.backend.websocket.scanner;

import com.alogic.load.Scanner;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.backend.websocket.WSEndpointInfo;
import com.logicbus.backend.websocket.WSEndpointScanner;

import java.util.ArrayList;
import java.util.List;

public class Sink extends Scanner.Sinkable<WSEndpointInfo> implements WSEndpointScanner,Scanner.Listener<WSEndpointInfo> {

    protected long ttl = 30 * 60 * 1000L;
    protected long lastScanTimestamp = 0;
    protected List<WSEndpointInfo> objList = new ArrayList<WSEndpointInfo>();

    @Override
    public void configure(Properties p){
        super.configure(p);
        ttl = PropertiesConstants.getLong(p,"ttl",ttl);
    }

    @Override
    public List<WSEndpointInfo> getList() {
        long now = System.currentTimeMillis();
        if (now - lastScanTimestamp > ttl){
            scan(this);
        }
        return objList;
    }

    @Override
    public Object begin(String scannerId) {
        return this;
    }

    @Override
    public void found(Object cookie, WSEndpointInfo data) {
        objList.add(data);
    }

    @Override
    public void end(Object cookie, String scannerId) {

    }
}
